<?php
namespace Admin\Controller;

class MenuGoodsController extends CommonController
{

    /**
     * 列表
     *
     * @param  void
     * @return void
     */
    public function index()
    {
        if (IS_POST) {
            $this->delete();
        } else {
            $get = I('get.');
            if (empty($get['menu_id'])) {
                $this->error('无效的数据');
            }
            $menu = M('Menu')->where('menu_id = ' . $get['menu_id'])->find();
            if (empty($menu)){
                $this->error('无效的数据');
            }
            $where['menu_id'] = $menu['menu_id'];
            $where['status'] = 1;
            $list = $this->lists(M('MenuGoods'), true, $where, 'id DESC', 10);
            $store = getStoreList(false);
            $area = getArea(false);
            $goods = getGoodsList(false);
            $this->assign('store', $store);
            $this->assign('list', $list);
            $this->assign('menu', $menu);
            $this->assign('area', $area);
            $this->assign('goods', $goods);
            $this->assign('get', $get);
            $this->display();
        }
    }
    
    /**
     * 新增
     *
     * @param  void
     * @return void
     */
    public function add()
    {
        $goods = getGoodsList(false);
        if (IS_POST) {
            $data = I('post.');

            $data['store_id'] = $goods[$data['goods_id']]['store_id'];
            $model = D('MenuGoods');
            $result = $model->update($data);
            if (false !== $result) {
                $this->success('保存成功', U('index', ['menu_id' => $data['menu_id']]));
            } else {
                $error = $model->getError();
                $this->error(empty($error) ? '未知错误' : $error);
            }
        } else {
            $menuId = I('get.menu_id');
            if (empty($menuId)) {
                $this->error('无效的数据');
            }
            $menu = M('Menu')->where('menu_id = ' . $menuId)->find();
            if (empty($menu)){
                $this->error('无效的数据');
            }
            $area = getArea(false);
            $store = getStoreList(false);
            $this->assign('store', $store);
            $this->assign('menu', $menu);
            $this->assign('area', $area);
            $this->assign('goods', $goods);
            $this->display();
        }
    }

    /**
     * 修改
     *
     * @param void
     * @return void
     */
    public function edit()
    {
        $goods = getGoodsList(false);
        if (IS_POST) {
            $data = I('post.');

            $data['store_id'] = $goods[$data['goods_id']]['store_id'];
            $model = D('MenuGoods');
            $result = $model->update($data);
            if (false !== $result) {
                $this->success('保存成功', U('index', ['menu_id' => $data['menu_id']]));
            } else {
                $error = $model->getError();
                $this->error(empty($error) ? '未知错误' : $error);
            }
        } else {
            $id = I('get.id');
            if (empty($id)) {
                $this->error('无效的数据');
            }
            $menuId = I('get.menu_id');
            if (empty($menuId)) {
                $this->error('无效的数据');
            }
            $menu = M('Menu')->where('menu_id = ' . $menuId)->find();
            if (empty($menu)){
                $this->error('无效的数据');
            }

            $info = M('MenuGoods')->where('id = ' . $id)->find();
            $area = getArea(false);
            $store = getStoreList(false);
            $this->assign('store', $store);
            $this->assign('menu', $menu);
            $this->assign('area', $area);
            $this->assign('goods', $goods);
            $this->assign('info', $info);
            $this->display();
        }
    }

    /**
     * 删除
     *
     * @param void
     * @return void
     */
    public function delete()
    {
        $id = array_unique((array) I('id', 0));
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $where = array( 'id' => array( 'in', $id ) );
        $data = array( 'status' => 0, 'update_time' => time() );
        $result = M('MenuGoods')->where($where)->save($data);
        if ($result) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }  
    
}
